/*
* Copyright (c) 2011 Connection Road.  All rights reserved.
* This program and the accompanying material are made available
* under the terms of the Eclipse Public Licnese v1.0 which is
* available at http://www.eclipse.org/legal/epl-v10.html
*/

using System;
using System.Collections.Generic;
using System.Xml.Serialization;

namespace CrSyncFilesUtils
{
    /// <summary>
    /// Custom serializable dictionary for storing various objects as value and any string as a key.
    /// </summary>
    
    [Serializable]
    public class CrSyncFilesCustomDictionary
    {

        public CrSyncFilesCustomDictionary()
        {
            Keys = new List<string>();
            Values = new List<object>();
        }

        public CrSyncFilesCustomDictionary(Dictionary<string,object> dictionary)
        {
            var keys = dictionary.Keys;
            var values = dictionary.Values;

            var newKeys = new string[keys.Count];
            var newValues = new object[values.Count];

            keys.CopyTo(newKeys,0);
            values.CopyTo(newValues,0);

            Keys = new List<string>(newKeys);
            Values = new List<object>(values);
        }

        public List<string> Keys { get; set; }
        public List<object> Values { get; set; }

        [XmlIgnore]
        public int CurrentIndex { get; set; }

        [XmlIgnore]
        public object CurrentValue { get; set; }

        public bool ContainsKey(string keyName)
        {
            if(Keys.Contains(keyName))
            {
                CurrentIndex = Keys.IndexOf(keyName);
                CurrentValue = Values[CurrentIndex];
                return true;
            }
            return false;
        }

        public void AddObjectForKey(object anObject, string aKey)
        {
            if(ContainsKey(aKey))
            {
                Values[CurrentIndex] = anObject;
            }
            else
            {
                Keys.Add(aKey);
                Values.Add(anObject);
            }
        }

        public object GetObjectForKey(string keyName)
        {
            return ContainsKey(keyName) ? CurrentValue : null;
        }

        public bool ContainsObject(object anObject)
        {
            if(Values.Contains(anObject))
            {
                CurrentIndex = Values.IndexOf(anObject);
                CurrentValue = anObject;
                return true;
            }
            return false;
        }

       
        public Dictionary<string,object > ToDictionary()
        {
            var dictionary = new Dictionary<string, object>();
            foreach (var key in Keys)
            {
                var index = Keys.IndexOf(key);
                dictionary.Add(key,Values[index]);
            }
            return dictionary;
        }

        public bool RemoveObjectForKey(string keyName)
        {
            if(ContainsKey(keyName))
            {
                Values.RemoveAt(CurrentIndex);
                Keys.Remove(keyName);
                return true;
            }
            return false;
        }
    }
}
